package com.begye.smartloan.apptest.outbefore

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.databinding.AtOutRecordBinding
import com.begye.smartloan.apptest.ext.addUnderLine
import com.begye.smartloan.apptest.kefu.KeFuDialogFragment
import com.begye.smartloan.apptest.gongong.YongHuJiDingYiViewModel
import com.begye.smartloan.apptest.tool.addTouch


@SuppressLint("CustomWelcomeScreen")
class OutRecordBeforeActivity : AppCompatActivity() {

    private lateinit var _binding: AtOutRecordBinding
    private lateinit var _outRecordBeforeAdapter: OutRecordBeforeAdapter

    private val _yongHuJiDingYiViewModel by lazy {
        ViewModelProvider(this)[YongHuJiDingYiViewModel::class.java]
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        _binding = AtOutRecordBinding.inflate(layoutInflater)
        setContentView(_binding.root)

        configYongHuLiuChengState()
        configUIState()
        configUIEvent()
        configUIData()
    }


    @SuppressLint("NotifyDataSetChanged")
    private fun configYongHuLiuChengState() {
        _yongHuJiDingYiViewModel.onCheckRecordsDataLiveData.observe(this) {
            it?.let {
                if(it.isEmpty()){
                    _binding.atOutRecordBeforeEmpty.visibility = View.VISIBLE
                }else{
                    _binding.atOutRecordBeforeEmpty.visibility = View.GONE

                    _outRecordBeforeAdapter.dataList = it
                    _outRecordBeforeAdapter.notifyDataSetChanged()
                }
            }
        }
    }

    private fun configUIState() {

    }

    private fun configUIEvent() {

        _binding.atOutRecordBeforeNar.initConfig(
            text = getString(R.string.out_record_text_7),
            textColor = "#000000",
            leftResId = R.drawable.dres_39,
            rightResId = R.drawable.dres_38
        ) {
            if (0 == it) {
                finish()
            } else if (1 == it) {
                val dialog = KeFuDialogFragment()
                dialog.showNow(supportFragmentManager, "kefu")
            }
        }

        _outRecordBeforeAdapter = OutRecordBeforeAdapter(emptyList())
        _binding.atOutRecordBeforeList.layoutManager = LinearLayoutManager(this)
        _binding.atOutRecordBeforeList.adapter = _outRecordBeforeAdapter

        _binding.atOutRecordBeforeEmptyAction.addUnderLine()

        _binding.atOutRecordBeforeEmptyAction.setOnClickListener { finish() }
    }

    private fun configUIData() {
        _yongHuJiDingYiViewModel.requestYongHuJiDingYiForRecords()
    }

    override fun onResume() {
        super.onResume()

        addTouch(type = this.javaClass.simpleName, isOpen = true)
    }

    override fun onPause() {
        super.onPause()

        addTouch(type = this.javaClass.simpleName, isComplete = true)
    }
}

